package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.*;
import coms.dto.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import coms.interceptors.UserAware;
import java.util.Map;

public class PublicationReviewAction extends ComsActionSupport implements UserAware {

    private User user;
    private int publicationId;
    private Publication publication;
    private PublicationDelegate publicationDelegate;
    private List<Review> reviewList;
    private ReviewDelegate reviewDelegate;
    private boolean admin;

    public boolean isAdmin() {
       User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public void assignSession() {
        Map<String, Object> sessionMap = getSession();
        User u = (User) sessionMap.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }

    }

    public PublicationReviewAction() {
        publication = new Publication();
        publicationDelegate = new PublicationDelegate();

        reviewList = new ArrayList<Review>();
        reviewDelegate = new ReviewDelegate();

    }

    public String getPublicationId() {
        return "" + publicationId;
    }

    public void setPublicationId(String publicationId) {
        try {
            if (publicationId != null && !publicationId.equals("")) {
                this.publicationId = Integer.parseInt(publicationId);
                publication.setId(this.publicationId);
            }
        } catch (Exception ex) {
            Logger.getLogger(PublicationReviewAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public User getUser() {
        return this.user;
    }

    @Override
    public void setUser(User user) {
        this.user = user;
    }

    public String viewReviewList() {
        assignSession();
        try {
            publication = publicationDelegate.getById(publicationId);
            reviewList = reviewDelegate.findByPublication(publication);
        } catch (Exception ex) {
            return "error";
        }
        return "viewReviewList";

    }

    public String viewCompiledReview() {
        assignSession();
        try {
            publication = publicationDelegate.getById(publicationId);
            reviewList = reviewDelegate.findByPublication(publication);
        } catch (Exception ex) {
            return "error";
        }
        return "viewCompiledReview";
    }

    public Publication getPublication() {
        return publication;
    }

    public void setPublication(Publication publication) {
        this.publication = publication;
    }

    public List<Review> getReviewList() {
        return reviewList;
    }

    public void setReviewList(List<Review> reviewList) {
        this.reviewList = reviewList;
    }
}
